﻿using Lcygcc.Infrastructure.Logs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lcygcc.Infrastructure.Core
{
    /// <summary>
    /// 
    /// </summary>
    public class TaskJobManager : ITaskJobManager
    {
        private readonly IAutoTaskThread mAutoTaskThread = null;

        private List<RequestArgs> mRequestArgs = new List<RequestArgs>();

        public List<RequestArgs> RequestArgsList
        {
            get
            {
                return mRequestArgs;
            }
            set
            {
                mRequestArgs = value;
            }
        }

        public TaskJobManager(IAutoTaskThread autoTaskThread)
        {
            
            mAutoTaskThread = autoTaskThread;
            InitTask();
        }



        /// <summary>
        /// 启动任务
        /// </summary>
        public void Start()
        {
            
            mAutoTaskThread.Start();
        }

        /// <summary>
        /// 加载定时任务
        /// </summary>
        private void InitTask()
        {
            //RequestArgs args = new RequestArgs();
            //args.ServiceID = "SyncPersonAdapter";
            //args.ServiceName = "从Oracle同步人事资料";
            //args.Cron = "0/50 * * * * ?";
            //args.AdapterMethod = "Execute";
            //mRequestArgs.Add(args);

            //args = new RequestArgs();
            //args.ServiceID = "queryCardSeriesList";
            //args.ServiceName = "查询金科卡系列";
            //args.Cron = "0/50 * * * * ?";
            //args.AdapterMethod = "Execute";
            //mRequestArgs.Add(args);

            //args = new RequestArgs();
            //args.ServiceID = "queryCardTypeList";
            //args.ServiceName = "查询金科卡分类";
            //args.Cron = "0/5 * * * * ?";
            //args.AdapterMethod = "Execute";
            //mRequestArgs.Add(args);

            if (EnvironmentInfo.TaskCustomInfoList.Count > 0)
            {
                EnvironmentInfo.TaskCustomInfoList.ForEach(it =>{
                    RequestArgs args = new RequestArgs();
                    args.ServiceID = it.ServiceID;
                    args.ServiceName = it.ServiceName;
                    args.Cron = it.Cron;
                    args.AdapterMethod = it.AdapterMethod;
                    RequestArgs temp = mRequestArgs.Where(x => x.ServiceID.Contains(args.ServiceID)).Select(x => x).FirstOrDefault();
                    if (temp == null)
                    {
                        mRequestArgs.Add(args);
                    }
                });
            }

            

            Logger.Info(string.Format("----加载定时任务完成：任务数量：{0}----", mRequestArgs.Count));
        }
    }
}
